<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商家" prop="merchId">
        <!-- <el-input
          v-model="queryParams.merchId"
          placeholder="请输入商户ID"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.merchId" placeholder="请选择作者">
          <el-option
            v-for="item in viewList2"
            :key="item.merchId"
            :label="item.merchName"
            :value="item.merchId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属年月" prop="monthTime">
        <!-- <el-input
          v-model="queryParams.monthTime"
          placeholder="请输入所属年月"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-date-picker
          clearable
          v-model="queryParams.monthTime"
          type="month"
          value-format="yyyy-MM"
          placeholder="请选择年月"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:view:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:view:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:view:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:view:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="viewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="商户ID" align="center" prop="merchId" />
      <el-table-column label="编号" align="center" prop="orderId" />
      <el-table-column label="商品ID" align="center" prop="proId" />
      <el-table-column label="所属年月" align="center" prop="monthTime" /> -->
      <el-table-column label="商家名称" align="center" prop="merchName"></el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishTime" />
      <el-table-column label="纯收入" align="center" prop="incomePrice" />
      <el-table-column label="订单总金额" align="center" prop="orderIncome" />
      <el-table-column label="售后退款金额" align="center" prop="refundAmount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:view:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:view:remove']"
          >删除</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:view:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改VIEW对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="订单详情" :visible.sync="open2" append-to-body>
      <el-form ref="form" :model="form2" :rules="rules" label-width="80px">
        <!-- <el-steps v-if="form2.orderStatus<11" :space="200" :active="form2.orderStatus" finish-status="success">
          <el-step title="未支付"></el-step>
          <el-step title="待出发"></el-step>
          <el-step title="待商户确认"></el-step>
          <el-step title="待用户确认"></el-step>
          <el-step v-if="form2.orderStatus==4" title="已完成" status="success"></el-step>
          <el-step v-else title="已完成"></el-step>
        </el-steps>
        <el-tag v-else type="info">订单已取消</el-tag> -->
        <div style="display: flex; justify-content: space-between" class="mt10">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>订单信息</span>
            </div>
            <div class="item">
              <span>订单号:{{ form2.orderCode }}</span>
            </div>
            <div class="item">
              <span>订单状态:{{ orderStatus[form2.orderStatus] }}</span>
            </div>
            <div class="item">
              <span>支付时间:{{ form2.payTime }}</span>
            </div>
            <div class="item">
              <span>订单金额:{{ form2.orderPrice }}</span>
            </div>
            <div class="item">
              <span v-if="form2.isSettle==0">结算状态:未结算</span>
              <span v-if="form2.isSettle==1">结算状态:已结算</span>
              <span v-if="form2.isSettle==2">结算状态:售后中</span>
            </div>
          </el-card>

          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>用户信息</span>
            </div>
            <div class="item">
              <span>联系人:{{ userAddr.realName }}</span>
            </div>
            <div class="item">
              <span>电话:{{ userAddr.phone }}</span>
            </div>
            <div class="item">
              <span
                >地址:{{ userAddr.userAddr }} - {{ userAddr.houseNumber }}</span
              >
            </div>
            <div class="item">
              <span>期望时间:{{ form2.timePeriod }}</span>
            </div>
          </el-card>
        </div>
        <div style="display: flex; justify-content: space-between" class="mt10">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>商品信息</span>
            </div>
            <div v-for="(item,index) in orderDetails" :key="index">
              <div class="item">
                <span>商品名称:{{ item.name }}</span>
              </div>
              <div class="item">
                <span>商品价格:{{ item.price }}</span>
              </div>
              <div class="item">
                <span>购买数量:{{ item.buyCount }}</span>
              </div>
              <div class="item">
                <span>总计金额:{{ item.price*item.buyCount }}</span>
              </div>
            </div>
          </el-card>
          <el-card class="box-card" v-if="provider!=null">
            <div slot="header" class="clearfix">
              <span>商家信息</span>
            </div>
            <div class="item">
              <span>商家名称:{{ provider.account }}</span>
            </div>
            <div class="item">
              <span>预计上门时间:{{ form2.estimateTime }}</span>
            </div>
            <div class="item">
              <span>联系人姓名:{{ provider.provName }}</span>
            </div>
            <div class="item">
              <span>联系人电话:{{ provider.phone }}</span>
            </div>
          </el-card>
        </div>
      </el-form>
      
      <div class="" v-if="orderAppend != null">
        <el-card style="margin-top: 20px">
          <div slot="header" class="clearfix">
            <span>追加服务</span>
          </div>
          <div>
            <span>追加订单号:{{ orderAppend.appendCode }}</span>
          </div>
          <div class="mt10">
            <span>追加金额:￥{{ orderAppend.price }}</span>
          </div>
          <div class="mt10">
            <span>追加内容：</span>
          </div>
          <div class="mt10">
            <span>{{ orderAppend.describe }}</span>
          </div>
          <div class="mt10" v-if="form2.sceneImages">
            <image-preview
              v-for="(item, index) in form2.sceneImages.split(',')"
              :src="api_host + item"
              :width="100"
              :height="100"
            />
          </div>
        </el-card>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('1')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listView, getView, delView, addView, updateView } from "@/api/system/income";
import {
  getOrder
} from "@/api/system/order";

export default {
  name: "View",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // VIEW表格数据
      viewList: [],
      viewList2:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchId: null,
        monthTime: null,
      },
      orderStatus: {
        0: "未支付",
        1: "待出发",
        2: "待商户确认",
        3: "待用户确认",
        4: "已完成",
        11: "已取消",
        22: "售后",
        33: "售后完成",
      },
      // 表单参数
      form: {},
      open2:false,
      form2:{},
      userAddr:{},
      orderAppend:{},
      orderDetails:[],
      provider:{},
      user:{},
      stepScore2: [],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getList2()
  },
  methods: {
    /** 查询VIEW列表 */
    getList() {
      this.loading = true;
      listView(this.queryParams).then(response => {
        this.viewList = response.rows;
        // this.viewList2 = this.distinct(response.rows,"merchId")
        this.total = response.total;
        this.loading = false;
      });
    },
    getList2() {
      this.loading = true;
      listView().then((response) => {
        this.viewList2 = this.distinct(response.rows,"merchId")
        this.loading = false;
      });
    },
    //去重
    distinct(arr, key) {
      let newobj = {}; 
      arr = arr .reduce((preVal, curVal) => {
          newobj[curVal[key]] ? '' : newobj[curVal[key]] = preVal.push(curVal); 
          return preVal 
      }, []);
      return arr;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        merchId: null,
        orderId: null,
        proId: null,
        monthTime: null,
        finishTime: null,
        incomePrice: null,
        orderIncome: null,
        refundAmount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.merchId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加VIEW";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const merchId = row.merchId || this.ids
      getView(merchId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改VIEW";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.merchId != null) {
            updateView(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addView(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const merchIds = row.merchId || this.ids;
      this.$modal.confirm('是否确认删除VIEW编号为"' + merchIds + '"的数据项？').then(function() {
        return delView(merchIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/view/export', {
        ...this.queryParams
      }, `view_${new Date().getTime()}.xlsx`)
    },
    handleView(row){
      console.log(row);
      // this.$router.push({
      //   path:'/order/order',
      //   query:{
      //     id:row.orderId
      //   }
      // })
      const id = row.orderId
      getOrder(id).then((response)=>{
        this.form2 = response.data;
        this.userAddr = response.data.userAddr;
        this.orderAppend = response.data.orderAppend;
        this.orderDetails = response.data.orderDetails
        this.provider = response.data.provider
        this.open2 = true;
      })
    }
  }
};
</script>

<style scope>
.box-card {
  width: 450px;
}
.item {
  margin-bottom: 18px;
}
</style>
